package com.arvato.common.util;

import javax.xml.parsers.DocumentBuilderFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.dom4j.DocumentHelper;

import com.arvato.wxwork.bean.bean_dto.WxworkCustomerDto;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.mapper.MapperWrapper;
import com.thoughtworks.xstream.security.AnyTypePermission;

import jxl.demo.XML;

import org.dom4j.DocumentException;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 * @Description XStream实现xml和对象互相转换的工具
 * @ClassName XStreamUtil
 * @author ding
 * @date
 *
 */
public class XStreamUtil {
	private static String XML_TAG = "<?xml version='1.0' encoding='UTF-8'?>";

	/**
	 * Description: 私有化构造
	 */
	private XStreamUtil() {
		super();
	}

	/**
	 * @Description 为每次调用生成一个XStream
	 * @Title getInstance
	 * @return
	 */
	private static XStream getInstance() {
		XStream xStream = new XStream(new DomDriver("UTF-8")) {
			/**
			 * 忽略xml中多余字段
			 */
			@Override
			protected MapperWrapper wrapMapper(MapperWrapper next) {
				return new MapperWrapper(next) {
					@SuppressWarnings("rawtypes")
					@Override
					public boolean shouldSerializeMember(Class definedIn, String fieldName) {
						if (definedIn == Object.class) {
							return false;
						}
						return super.shouldSerializeMember(definedIn, fieldName);
					}
				};
			}
		};

		// 设置默认的安全校验
		// 使用本地的类加载器
		xStream.setClassLoader(XStreamUtil.class.getClassLoader());
		// 允许所有的类进行转换
		xStream.addPermission(AnyTypePermission.ANY);
		return xStream;
	}

	/**
	 * @Description 将xml字符串转化为java对象
	 * @Title xmlToBean
	 * @param xml
	 * @param clazz
	 * @return
	 */
	public static <T> T xmlToBean(String xml, Class<T> clazz) {
		XStream xStream = getInstance();
		xStream.processAnnotations(clazz);
		Object object = xStream.fromXML(xml);
		T cast = clazz.cast(object);
		return cast;
	}

	/**
	 * @Description 将java对象转化为xml字符串
	 * @Title beanToXml
	 * @param object
	 * @return
	 */
	public static String beanToXml(Object object) {
		XStream xStream = getInstance();
		xStream.processAnnotations(object.getClass());
		// 剔除所有tab、制表符、换行符
		String xml = xStream.toXML(object).replaceAll("\\s+", " ");
		return xml;
	}

	/**
	 * @Description 将java对象转化为xml字符串（包含xml头部信息）
	 * @Title beanToXml
	 * @param object
	 * @return
	 */
	public static String beanToXmlWithTag(Object object) {
		String xml = XML_TAG + beanToXml(object);
		return xml;
	}

	public static String createXml(WxworkCustomerDto dto) {
		String xmlString = "";
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.newDocument();
			document.setXmlStandalone(true);

			Element itemInfo = document.createElement(dto.getName());
			document.appendChild(itemInfo);

			Element itemOwnerId = document.createElement("OwnerId");
			itemOwnerId.setTextContent(dto.getOwnerId());
			itemInfo.appendChild(itemOwnerId);
			// 此处可以循环添加
			Element itemStoreId = document.createElement("StoreId");
			itemStoreId.setTextContent(dto.getStoreId());
			itemInfo.appendChild(itemStoreId);

			Element itemNameMemberNo = document.createElement("MemberNo");
			itemNameMemberNo.setTextContent(dto.getMemberNo());
			itemInfo.appendChild(itemNameMemberNo);

			TransformerFactory transFactory = TransformerFactory.newInstance();
			Transformer transformer = transFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			DOMSource domSource = new DOMSource(document);

			// xml transform String
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			transformer.transform(domSource, new StreamResult(bos));
			xmlString = bos.toString();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return xmlString;
	}

	  
	
	
	

}
